<template>
  <div class='login-page'>
    <div class="left">
      <h1>我的医疗管理平台</h1>
      <p>Copyright 2021 格凡安信科技有限公司 版权所有</p>
    </div>
    <div class="right">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <h2>登录</h2>
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" size="medium" placeholder="用户名">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="ruleForm.password" type="password" size="medium" placeholder="密码">
          </el-input>
        </el-form-item>
        <el-form-item prop="captcha">
          <div class="captcha">
            <el-input v-model="ruleForm.captcha" size="medium" placeholder="验证码"></el-input>
            <img :src="imgUrl" alt="" @click="getCaptcha">
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" size="medium" style="width: 100%;">立即创建</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/http'
import { getUUID } from '@/utils'
export default {
  name: 'login-page',
  /**
  * 定义组件的data函数，用于声明组件内部的状态。
  * 
  * @returns {Object} 返回一个对象，其中包含组件内部的状态。这些状态将在组件的生命周期内被管理和使用。
  */
  data() {
    return {
      ruleForm: {
        uuid: '',
      },
      baseUrl: 'http://123.207.219.13:38080/captcha.jpg',
      imgUrl: '',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.getCaptcha()
  },
  methods: {
    
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = Object.assign({}, this.ruleForm);
          params.password = this.$md5(this.ruleForm.password)
          loginAPI(params).then(res => {
            // console.log(res);
            if (res.code === 200) {

              sessionStorage.setItem('menuList', JSON.stringify(res.data.userInfo.menus || '{}')) // 菜单存储
              sessionStorage.setItem('token', res.data.token)
              sessionStorage.setItem('userInfo', JSON.stringify(res.data.userInfo))

              this.$router.push('/home');
              this.$message({
                showClose: true,
                message: '登录成功',
                type: 'success'
              });
            } else {
              this.getCaptcha()
              this.$message({
                showClose: true,
                message: res.msg,
                type: 'warning'
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    getCaptcha() {
      this.ruleForm.uuid = getUUID()
      this.imgUrl = `${this.baseUrl}?uuid=${this.ruleForm.uuid}`
    }
  }
}
</script>
<style scoped lang="less">
.login-page {
  display: flex;
  background-image: url(~@/assets/images/login_bg.jpg);
  background-size: cover;

  .left {
    flex-grow: 1;
    height: 100vh;
    background-color: rgba(38, 50, 56, 0.6);
    padding: 100px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-bottom: 250px;

    h1 {
      font-family: '黑体';
      font-size: 48px;
      flex-wrap: nowrap;
      color: #fff;
    }

    p {
      font-size: 16px;
      color: #ddd;
    }
  }

  .right {
    width: 470px;
    height: 100vh;
    background-color: #fff;
    box-sizing: border-box;
    padding: 0 60px;
    display: flex;
    align-items: center;

    .demo-ruleForm {
      width: 100%;

      h2 {
        font-size: 18px;
        margin-bottom: 10px;
      }

      .captcha {
        display: flex;
        align-items: center;

        img {
          width: 134px;
          height: 33px;
          margin-left: 10px;
        }
      }
    }
  }
}
</style>